//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/image/algorithm/gaussian_map.h>

namespace buola { namespace img {

void gaussian_map(const CMView_rgba8 &pDst,const CSize_d &pRadius,const CPixel_rgba8 &pMaxValue,const CPoint_d &pCenter)
{
    double lX[pDst.Width()];
    double lY[pDst.Height()];

    if(pDst.Width()==pDst.Height()&&pRadius.x==pRadius.y&&pCenter.x==pCenter.y)
    {
        for(int i=0;i<pDst.Width();i++)
        {
            lX[i]=(1.0+erf((i-pCenter.x+pRadius.x)/sqrt(2)/(pRadius.x/3)))/2.0;
            lY[i]=lX[i];
        }
    }
    else
    {
        for(int i=0;i<pDst.Width();i++)
        {
            lX[i]=(1.0+erf((i-pCenter.x+pRadius.x)/sqrt(2)/(pRadius.x/3)))/2.0;
        }
        for(int i=0;i<pDst.Height();i++)
        {
            lY[i]=(1.0+erf((i-pCenter.y+pRadius.y)/sqrt(2)/(pRadius.y/3)))/2.0;
        }
    }

    if(pMaxValue[0]==0&&pMaxValue[1]==0&&pMaxValue[2]==0)
    {
        double lA=pMaxValue[3];
        
        for(int y=0;y<pDst.Height();y++)
        {
            double lAY=lA*lY[y];
            auto lIt=pDst.Row(y).begin();
            for(auto x=0;x<pDst.Width();x++)
            {
                lIt[x][0]=0;
                lIt[x][1]=0;
                lIt[x][2]=0;
                lIt[x][3]=(unsigned char)(lAY*lX[x]+0.5);
            }
        }
    }
    else
    {
        double lR=pMaxValue[0];
        double lG=pMaxValue[1];
        double lB=pMaxValue[2];
        double lA=pMaxValue[3];
        
        for(int y=0;y<pDst.Height();y++)
        {
            double lRY=lR*lY[y];
            double lGY=lG*lY[y];
            double lBY=lB*lY[y];
            double lAY=lA*lY[y];
            auto lIt=pDst.Row(y).begin();
            for(auto x=0;x<pDst.Width();x++)
            {
                lIt[x][0]=(unsigned char)(lRY*lX[x]+0.5);
                lIt[x][1]=(unsigned char)(lGY*lX[x]+0.5);
                lIt[x][2]=(unsigned char)(lBY*lX[x]+0.5);
                lIt[x][3]=(unsigned char)(lAY*lX[x]+0.5);
            }
        }
    }
}
    
void gaussian_map(const CMView_gray8 &pDst,const CSize_d &pRadius,uint8_t pMaxValue,const CPoint_d &pCenter)
{
    double lX[pDst.Width()];
    double lY[pDst.Height()];

    if(pDst.Width()==pDst.Height()&&pRadius.x==pRadius.y&&pCenter.x==pCenter.y)
    {
        for(int i=0;i<pDst.Width();i++)
        {
            lX[i]=(1.0+erf((i-pCenter.x+pRadius.x)/sqrt(2)/(pRadius.x/3)))/2.0;
            lY[i]=lX[i];
        }
    }
    else
    {
        for(int i=0;i<pDst.Width();i++)
        {
            lX[i]=(1.0+erf((i-pCenter.x+pRadius.x)/sqrt(2)/(pRadius.x/3)))/2.0;
        }
        for(int i=0;i<pDst.Height();i++)
        {
            lY[i]=(1.0+erf((i-pCenter.y+pRadius.y)/sqrt(2)/(pRadius.y/3)))/2.0;
        }
    }

    double lA=pMaxValue;
    
    for(int y=0;y<pDst.Height();y++)
    {
        double lAY=lA*lY[y];
        auto lIt=pDst.Row(y).begin();
        for(auto x=0;x<pDst.Width();x++)
        {
            lIt[x]=(unsigned char)(lAY*lX[x]+0.5);
        }
    }
}
    
/*namespace image*/ } /*namespace buola*/ }
